package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约管理功能
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //预约设置信息管理
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        //健壮性判断
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //检查日期是否存在
                long count = orderSettingDao.findDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //如果存在进行修改
                    orderSettingDao.setOrderSetting(orderSetting);
                } else {
                    //不存在进行添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份获取数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";//格式:2020-6-1
        String end = date + "-31";//格式:2020-6-31
        //声明一个map集合将拼接后完整年月日存入
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Map ordersettingmap = new HashMap<>();
                ordersettingmap.put("date", orderSetting.getOrderDate().getDate());//获取日期几号
                ordersettingmap.put("number", orderSetting.getNumber());//获取可以预约人数
                ordersettingmap.put("reservations", orderSetting.getReservations());//获取已经预约人数
                data.add(ordersettingmap);
            }
        }
        return data;
    }

    //预约设置
    @Override
    public void setOrderSetting(OrderSetting orderSetting) {
        long count = orderSettingDao.findDate(orderSetting.getOrderDate());
        //判断日期是否存在
        if (count > 0) {
            orderSettingDao.setOrderSetting(orderSetting);
        } else {
            //没有出现,进行添加
            orderSettingDao.add(orderSetting);
        }
    }
}
